package com.xh.jf_blog.service.impl;

import com.xh.jf_blog.bean.Anthology;
import com.xh.jf_blog.common.exception.RTException;
import com.xh.jf_blog.mapper.AnthologyMapper;
import com.xh.jf_blog.service.AnthologyService;
import com.xh.jf_blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/2/20/020
 */
@Service
@Transactional
public class AnthologyServiceImpl implements AnthologyService {
    @Autowired
    private AnthologyMapper anthologyMapper;
    @Autowired
    private ArticleService articleService;

    public List<Anthology> getSelect(int userinfoId) {
        Anthology anthology = new Anthology();
        anthology.setUserinfoId(userinfoId);
        return anthologyMapper.getAll(anthology);
    }

    public void saveOrUpdate(Anthology anthology) {
        if (anthology.getAnthologyId() == null) {
            anthologyMapper.insertSelective(anthology);
        } else {
            anthologyMapper.updateByPrimaryKeySelective(anthology);
        }
    }

    public void delete(int anthologyId) {
        if (!articleService.getListByAnthologyId(anthologyId).isEmpty()) {
            throw new RTException("该文集已被使用，无法删除！");
        }
        anthologyMapper.deleteByPrimaryKey(anthologyId);
    }

    public Anthology getById(int anthologyId) {
        return anthologyMapper.selectByPrimaryKey(anthologyId);
    }
}
